package cn.tedu.oa.user.service.impl;

import cn.tedu.oa.base.exception.ServiceException;
import cn.tedu.oa.base.response.StatusCode;
import cn.tedu.oa.user.mapper.UserMapper;
import cn.tedu.oa.user.pojo.dto.UserLoginParam;
import cn.tedu.oa.user.pojo.dto.UserLogoutParam;
import cn.tedu.oa.user.pojo.dto.UserQuery;
import cn.tedu.oa.user.pojo.dto.UserSaveParam;
import cn.tedu.oa.user.pojo.entity.User;
import cn.tedu.oa.user.pojo.vo.UserVO;
import cn.tedu.oa.user.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.Period;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;

/* 业务层实现类需要添加@Service注解,表示该类为业务层实现类 */
@Slf4j
@Service
public class UserServiceImpl implements UserService {
    @Autowired
    UserMapper userMapper;

    @Override
    public UserVO login(UserLoginParam userLoginParam) {
        log.debug("登录业务,参数:{}",userLoginParam);
        //根据前端传来的用户名查询数据库中是否有此用户的数据 Alt+Enter
        UserVO userVO = userMapper.selectByUsername(userLoginParam.getUsername());
        /* 如果业务层遇到了问题,不可以直接给前端响应,要抛出异常,由全局异常处理器来处理! */
        if(userVO == null){
            throw new ServiceException(StatusCode.USERNAME_ERROR);
        }
        if(!userVO.getPassword().equals(userLoginParam.getPassword())){
            throw new ServiceException(StatusCode.PASSWORD_ERROR);
        }
        /* 如果没有抛出异常,业务层会将正常处理完毕的userVO结果返回给Controller层
        再由Controller层响应给前端 */
        return userVO;
    }

    @Override
    public void saveUser(UserSaveParam userSaveParam) {
        log.debug("保存用户业务,参数:{}",userSaveParam);
        User user = new User();
        BeanUtils.copyProperties(userSaveParam,user);
        // 获取司龄开始时间
        Date seniorityStart = user.getSeniorityStart();
        if (seniorityStart != null) {
            LocalDate seniorityStartDate = seniorityStart.toInstant().atZone(java.time.ZoneId.systemDefault()).toLocalDate();
            LocalDate now = LocalDate.now();
            Period period = Period.between(seniorityStartDate, now);
            int seniority = period.getYears();
            user.setSeniority(seniority);
        }
        // 需要判断是新增还是修改操作
        if (user.getId() == null) {
            user.setPassword("123456"); // 员工不是自己注册,所以需要设置初始密码
            user.setCreateTime(new Date()); // 设置创建时间为当前时间
            userMapper.insert(user); // 调用数据层新增员工
        } else {
            // TODO 更新用户
            user.setUpdateTime(new Date());
            userMapper.update(user);
        }
    }

    @Override
    public List<UserVO> selectUser(UserQuery userQuery) {
        log.debug("查询用户业务,参数:{}",userQuery);
        List<UserVO> list = userMapper.selectUser(userQuery);
        System.out.println(list);
        return list;
    }

    @Override
    public void resetPassword(Integer userId) {
        log.debug("重置密码业务,参数:{}",userId);
        User user = new User();
        user.setId(userId);
        user.setPassword("root");
        user.setUpdateTime(new Date());
        userMapper.update(user);
    }

    @Override
    public void updateStatus(Integer userId, String status) {
        log.debug("修改用户状态业务,参数:{},{}",userId,status);
        User user = new User();
        user.setId(userId);
        user.setStatus(status);
        user.setUpdateTime(new Date());
        userMapper.update(user);
    }

    @Override
    public void deleteUser(Integer userId) {
        log.debug("删除用户业务,参数:{}",userId);
        userMapper.deleteById(userId);
    }

    @Override
    public List<UserVO> selectAuditList(Integer parentId) {
        log.debug("查询审批列表业务,参数:{}",parentId);
        //1.准备一个空集合,用来保存查出来的多个审批人
        ArrayList<UserVO> userVOList = new ArrayList<>();
        //2.先根据传过来的上级id(其实也是用户id)将直属领导查出来,装到上方集合里
        UserVO auditUser1 = userMapper.selectById(parentId);
        userVOList.add(auditUser1);
        //3.判断是否有直属领导,且直属领导还有上级
        if(auditUser1 != null && auditUser1.getParentId() !=null){
            //4.根据直属领导的上级id查出上上级,并装入上方的空集合中
            UserVO auditUser2 = userMapper.selectById(auditUser1.getParentId());
            userVOList.add(auditUser2);
        }
        //5.将准备好的审批人集合返回给controller
        return userVOList;
    }

    @Override
    public List<UserVO> selectLeader(UserQuery userQuery) {
        log.debug("查询用户业务,参数:{}",userQuery);
        List<UserVO> list = userMapper.selectLeader(userQuery);
        System.out.println(list);
        return list;
    }

    @Override
    public void updateLogoutTime(UserLogoutParam userLogoutParam) {
        log.debug("更新用户在线时长,参数:{}",userLogoutParam);
        userLogoutParam.setDuration(userLogoutParam.getDuration()/3600+1);
        userMapper.updateLogoutTime(userLogoutParam);
    }

    @Override
    public List<UserVO> selectByDuration() {
        List<UserVO> list = userMapper.selectByDuration();
        return list;
    }

    @Override
    public Integer selectCount() {
        Integer count = userMapper.selectCount();
        return count;
    }


}







